package com.kkarma.util.service;

import com.kkarma.pojo.MmsManager;
import com.kkarma.service.MmsManagerService;
import com.kkarma.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Author: karma
 * @Date: 2022/3/30 0030 - 03 - 30 - 13:17
 * @Description: com.kkarma.util.service
 * @version: 1.0
 */
@Service
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private MmsManagerService managerService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if(StringUtils.isEmpty(username)) throw new UsernameNotFoundException("用户名不能为空");
        MmsManager mmsManager = managerService.selectManagerByUsername(username);
        if(null == mmsManager) throw new BadCredentialsException("用户名或密码错误");
        return createLoginUser(mmsManager);
    }

    public LoginUser createLoginUser(MmsManager mmsManager) {
        return new LoginUser(mmsManager.getManagerId(), mmsManager.getDeptId(), mmsManager);
    }
}
